import * as layout from "./assets/js/layout.js";
import * as files from "./assets/js/files.js";
var ENABLE_SERVER = 1;
export function postCallback(msg, success) {
  var parser = new DOMParser();
  var xmlDoc = parser.parseFromString(msg, "application/xml");
  var root = xmlDoc.documentElement;
  var nodes = root.getElementsByTagName("*");
  //console.log("root length:" + nodes.length);
  if (nodes.length == 0) {
    document.getElementById("notice").innerHTML = success;
  } else {
    var child = root.childNodes[0];
    var error = child.getAttribute("error");
    if (error) {
      document.getElementById("notice").innerHTML = error;
    } else {
      document.getElementById("notice").innerHTML = success;
    }
  }
}
export function postNew(elementIndex) {
  var target = event.target;
  //console.log('a.title:', target.id);
  //console.log('elementIndex:', elementIndex);
  var elementId = "post" + elementIndex;
  const e = document.getElementById(elementId);
  if (!e) {
    console.log("[WARNING] no this element:", elementId);
    return;
  }
  console.log('e.outerHTML:', e.outerHTML);
  var xmlString;
  xmlString = dom.node2xml_str(e);
  console.log('node data:', xmlString);
  xmlString = dom.node2sql_insert_xml_str(e);
  console.log('sql insert data:', xmlString);
  if (ENABLE_SERVER) {
    var client = new XMLHttpRequest();
    var url = '/sql';
    client.open("post", url);
    client.setRequestHeader ('content-type', 'text/plain');
    client.send(xmlString);
    console.log("[----] " + url + ':' + xmlString);
    client.onreadystatechange = function() {
      if(this.readyState == this.DONE) {
        console.log("[++++] " + url + ':' + client.responseText);
        postCallback(client.responseText, "[success] tings insert");
      }
    }
  } else {
    postCallback("<div></div>", "[success] tings insert");
  }//ENABLE_SERVER
}
export function form(elementId, msg) {
  const e = document.getElementById(elementId);
  if (!e) {
    console.log("[WARING] no this element:", elementId);
    return;
  }
  var parser = new DOMParser();
  var xmlDoc = parser.parseFromString(msg, "text/xml");
  var root = xmlDoc.documentElement;
  var child = root.childNodes[0];
  if (!child)
    return;
  var elementNode = dom.get_nextsibling(child);
  var children = root.childNodes;
  var rowCount = 0;
  for (const node of children) {
    var div = document.createElement("div");
    div.id = "post" + rowCount;
    e.appendChild(div);
    var attributes = node.attributes;
    if (!attributes)
      break;
    for (var j = 0; j < attributes.length; j++) {
      var attrName = attributes[j].name;  // 属性名
      var attrValue = attributes[j].value;  // 属性值
      //console.log("attrName:" + attrName + ", attrValue:" + attrValue);
      var input = document.createElement("input");
      input.id = attrName;// + rowCount;
      input.value = attrValue;
      div.appendChild(input);
    }
    var b = document.createElement("button");
    b.type = "submit";
    b.textContent = "submit";
    b.id = attrName + rowCount;
    b.value = attrValue;
    b.addEventListener('click', () => {
      postNew(rowCount);
      })
    div.appendChild(b);
    break;
    rowCount++;
  }
}
window.onload = function() {
  layout.standard();
  files.upload_form("article", "/files", "filesInput");
  if (ENABLE_SERVER) {
  } else {
  }
};

//window.addEventListener('resize', layout.updateFloatingHeight('nav', 0.8));
